package com.example.oppotest.controller;

import com.example.oppotest.entity.youku.AdInfo;
import com.example.oppotest.entity.youku.Effpoint;
import com.example.oppotest.service.EffpointService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author wyk
 * @Date: 2025/2/28 23:01
 */

@Controller
@RequestMapping("/page")
public class PageController {

    @Autowired
    private EffpointService effpointService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/landing/v1/Android/{effpointId}", method = RequestMethod.GET)
    public String landingAndroid(@PathVariable Integer effpointId, HttpServletRequest request) {
        String __OS__ = request.getParameter("__OS__"); // 操作系统
        String __TRACKID__ = request.getParameter("__TRACKID__"); // 追踪id
        String __CREATIVEID__ = request.getParameter("__CREATIVEID__"); // 创意id
        String __IMEI__ = request.getParameter("__IMEI__"); // IMEI（Android设备ID）
        String __OAID__ = request.getParameter("__OAID__"); // OAID（通用匿名设备标识）
        String __IP__ = request.getParameter("__IP__"); // IP地址
        String __IDFA__ = request.getParameter("__IDFA__"); // IDFA（iOS 广告ID）
        String __CAID__ = request.getParameter("__CAID__"); // 互联网广告标识

        logger.info(__OS__ + "," + __TRACKID__ + "," + __CREATIVEID__ + "," + __IMEI__ + "," + __OAID__
                + "," + __IP__ + "," + __IDFA__ + "," + __CAID__ + ",landing" + effpointId);

        return "youku/landing";
    }

    @RequestMapping(value = "/landing/v2/Android/{effpointId}", method = RequestMethod.GET)
    public String landingAndroid2(@PathVariable Integer effpointId, HttpServletRequest request,
                                   @RequestParam String os, @RequestParam String trackId, @RequestParam String creativeid,
                                   @RequestParam String imei,@RequestParam String oaid,@RequestParam String ip,
                                   @RequestParam String clickid,@RequestParam String mac,@RequestParam String androidid,
                                   @RequestParam String idfa,@RequestParam String caid,@RequestParam String ua) throws Exception {
        logger.info("os:" + os + "," + "trackId:" + trackId + "," + "creativeId:" + creativeid + "," + "imei:" + imei + "," + "oaid:" + oaid
                + "," + "ip:" + ip + "," + "idfa:" + idfa + "," + "clickid:" + clickid + "," + "mac:" + mac + "," + "androidid:" + androidid
                + "," + "caid:" + caid + "," + "ua:" + ua + ",landing" + effpointId);

        AdInfo adInfo = new AdInfo();
        adInfo.setEffpointId(effpointId);
        adInfo.setOs(os);
        adInfo.setTrackId(trackId);
        adInfo.setCreativeId(creativeid);
        adInfo.setImei(imei);
        adInfo.setOaid(oaid);
        adInfo.setIp(ip);
        adInfo.setIdfa(idfa);
        adInfo.setCaid(caid);
        adInfo.setUa(ua);
        adInfo.setIdfa(idfa);
        adInfo.setCaid(caid);
        adInfo.setUa(ua);
//        ModelMap map = effpointService.createEffpointString(adInfo, request);
//        logger.info((String) map.get("result"));

        return "youku/landing";
    }


    @RequestMapping(value = "/track/v1/Android/{effpointId}", method = RequestMethod.GET)
    public String trackAndroid(@PathVariable Integer effpointId, HttpServletRequest request) throws Exception {
        ModelMap map = new ModelMap();
        String __OS__ = request.getParameter("__OS__"); // 操作系统
        String __TRACKID__ = request.getParameter("__TRACKID__"); // 追踪id
        String __CREATIVEID__ = request.getParameter("__CREATIVEID__"); // 创意id
        String __IMEI__ = request.getParameter("__IMEI__"); // IMEI（Android设备ID）
        String __OAID__ = request.getParameter("__OAID__"); // OAID（通用匿名设备标识）
        String __IP__ = request.getParameter("__IP__"); // IP地址
        String __IDFA__ = request.getParameter("__IDFA__"); // IDFA（iOS 广告ID）
        String __CAID__ = request.getParameter("__CAID__"); // 互联网广告标识

        logger.info(__OS__ + "," + __TRACKID__ + "," + __CREATIVEID__ + "," + __IMEI__ + "," + __OAID__
                + "," + __IP__ + "," + __IDFA__ + "," + __CAID__ + ",track" + effpointId);

        if (effpointId != null && StringUtils.hasText(__CREATIVEID__) && StringUtils.hasText(__TRACKID__)) {
            AdInfo adInfo = new AdInfo();
            adInfo.setEffpointId(effpointId);
            adInfo.setOs(__OS__);
            adInfo.setTrackId(__TRACKID__);
            adInfo.setCreativeId(__CREATIVEID__);
            adInfo.setImei(__IMEI__);
            adInfo.setOaid(__OAID__);
            adInfo.setIp(__IP__);
            adInfo.setIdfa(__IDFA__);
            adInfo.setCaid(__CAID__);
            map = effpointService.createEffpointString(adInfo, request);
            logger.info((String) map.get("result"));
        }
        return "youku/landing";
    }

    @RequestMapping(value = "/track/v2/Android/{effpointId}", method = RequestMethod.GET)
    public String trackAndroid2(@PathVariable Integer effpointId, HttpServletRequest request,
                                  @RequestParam String os, @RequestParam String trackId, @RequestParam String creativeid,
                                  @RequestParam String imei,@RequestParam String oaid,@RequestParam String ip,
                                  @RequestParam String clickid,@RequestParam String mac,@RequestParam String androidid,
                                  @RequestParam String idfa,@RequestParam String caid,@RequestParam String ua) throws Exception {
        logger.info("os:" + os + "," + "trackId:" + trackId + "," + "creativeId:" + creativeid + "," + "imei:" + imei + "," + "oaid:" + oaid
                + "," + "ip:" + ip + "," + "idfa:" + idfa + "," + "clickid:" + clickid + "," + "mac:" + mac + "," + "androidid:" + androidid
                + "," + "caid:" + caid + "," + "ua:" + ua + ",landing" + effpointId);
        String url = "\n" +
                "https://www.xiaogao.site:441/page/track/v2/Android/8?ip=__IP__&clickid=__CLICKID__&imei=__IMEI__" +
                "&oaid=__OAID__&trackId=__TRACKID__&idfa=__IDFA__" +
                "&creativeid=__CREATIVEID__&os=__OS__&mac=__MAC__&androidid=__ANDROIDID__&caid=__CAID__&ua=__UA__";
        AdInfo adInfo = new AdInfo();
        adInfo.setEffpointId(effpointId);
        adInfo.setOs(os);
        adInfo.setTrackId(trackId);
        adInfo.setCreativeId(creativeid);
        adInfo.setImei(imei);
        adInfo.setOaid(oaid);
        adInfo.setIp(ip);
        adInfo.setClickid(clickid);
        adInfo.setMac(mac);
        adInfo.setAndroidid(androidid);
        adInfo.setIdfa(idfa);
        adInfo.setCaid(caid);
        adInfo.setUa(ua);
        ModelMap map = effpointService.createEffpointString(adInfo, request);
        logger.info((String) map.get("msg"));

        return "youku/landing";
    }


}
